﻿using System;
using UFIDA.U9.Cust.Pub.WSLogUI;
using UFSoft.UBF.UI.MD.Runtime;

/***********************************************************************************************
 * Form ID: 
 * UIFactory Auto Generator 
 ***********************************************************************************************/
namespace UFIDA.U9.Cust.Pub.SelectDateUIModel
{
    public partial class SelectDateUIFormWebPart
    {
        #region Custome eventBind

        //BtnClose_Click...
        private void BtnClose_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            BtnClose_Click_DefaultImpl(sender, e);
        }

        //BtnOk_Click...
        private void BtnOk_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            BtnOk_Click_DefaultImpl(sender, e);
            //确认
            DoConfirm();
        }

        #region 自定义数据初始化加载和数据收集

        private void OnLoadData_Extend(object sender)
        {
            OnLoadData_DefaultImpl(sender);
        }

        private void OnDataCollect_Extend(object sender)
        {
            OnDataCollect_DefaultImpl(sender);
        }

        #endregion

        #region 自己扩展 Extended Event handler 

        public void AfterOnLoad()
        {
            if (!Page.IsPostBack)
            {
                if (CurrentState[WSLogUIHelper.SelectDateDefaultValueSymbol] != null)
                {
                    DateTime date =
                        DateTime.Parse(CurrentState[WSLogUIHelper.SelectDateDefaultValueSymbol].ToString());
                    if (Model.DateView.FocusedRecord == null)
                    {
                        DateViewRecord dateViewRecord = Model.DateView.AddNewUIRecord();
                        Model.DateView.FocusedRecord = dateViewRecord;
                    }

                    Model.DateView.FocusedRecord.Date = date;
                }
            }
        }

        public void AfterCreateChildControls()
        {
        }

        public void AfterEventBind()
        {
        }

        public void BeforeUIModelBinding()
        {
        }

        public void AfterUIModelBinding()
        {
            BtnClose.IsClientClose = true;
        }

        #endregion

        #region 方法

        /// <summary>
        ///     确认
        /// </summary>
        private void DoConfirm()
        {
            Model.ClearErrorMessage();
            DateViewRecord focusedRecord = Model.DateView.FocusedRecord;
            if (focusedRecord == null || focusedRecord.Date == DateTime.MinValue)
            {
                SetErrorMessage(Model, focusedRecord, Model.DateView.FieldDate.Name, "日期不能为空");
                return;
            }

            CurrentState[WSLogUIHelper.SelectDateResultValueSymbol] = focusedRecord.Date;
            CloseDialog(true);
        }

        /// <summary>
        ///     错误提示
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="message"></param>
        /// <param name="model"></param>
        /// <param name="record"></param>
        public static void SetErrorMessage(IUIModel model, IUIRecord record, string fieldName, string message)
        {
            model.ErrorMessage.SetErrorMessage(ref record, fieldName, message);
            model.ErrorMessage.SetErrorMessage(ref model, message);
        }

        #endregion

        #endregion
    }
}